I want get two element from page and put into one object
const entryElements = await page.$$('div.kpi-entry');
const contents = await Promise.all(
entryElements.map(async (element) => {
const objValue= await (await element.$('span.text-xl'))?.innerText();
const objKey= await (await element.$('eui-base-v0-tooltip'))?.innerText();
const kpiObject = {objValue,objKey,};
return obj;
})
);
obj returns
{ objValue: '1', objKey: 'A' },
{ objValue: '2', objKey: 'B' },
{ objValue: '3', objKey: 'C' },
{ objValue: '4', objKey: 'D' },
{ objValue: '5', objKey: 'E' }
but I expected
{
A: '1',
B: '2',
C: '3',
D: '4',
E: '5',
}
what is the best wat to do this
I only adapted your own code, you should check MDN Working with Objects for more details.
const entryElements = await page.$$('div.kpi-entry');
const newObject = {};
const contents = await Promise.all(
entryElements.forEach(async (element) => {
const objValue= await (await element.$('span.text-xl'))?.innerText();
const objKey= await (await element.$('eui-base-v0-tooltip'))?.innerText();
newObject[objKey] = objValue;
})
console.log(newObject)
let obj = [
{ objValue: "1", objKey: "A" },
{ objValue: "2", objKey: "B" },
{ objValue: "3", objKey: "C" },
{ objValue: "4", objKey: "E" },
{ objValue: "5", objKey: "F" },
];
let map = obj.map(element => {
return { [element.objKey]: element.objValue };
});
console.log(Object.assign(...map));